﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Project.Database;
using Project.Database.Repository;
using Project.Models;

namespace Project.Controllers
{
    public class AdvertController : Controller
    {
        private DatabaseRepository entities = new DatabaseRepository();
        //
        // GET: /Advert/List

        public ActionResult List(int id = -1)
        {
            if (id >= 0)
            {

            }
            return View(entities.GetAllAdverts());
        }

        [Authorize(Roles = "Advertiser")]
        public ActionResult MyAds()
        {
            User user = entities.GetUser(User.Identity.Name);
            Advertiser advertiser = entities.GetAdvertiser(user);
            ViewBag.Adverts = entities.GetAdvertsForAdvertiser(advertiser);
            return View();
        }

        [Authorize(Roles = "Advertiser")]
        public ActionResult AddAdvert(int id = -1)
        {
            try
            {
                //check cid
                Advertiser advetiser = entities.GetAdvertiser(entities.GetUser(User.Identity.Name));
                AdCampain campaign = entities.GetAdCampaignForAdvertiser(advetiser, id);
                if (campaign == null)
                {
                    return RedirectToAction("InvalidCampaign", "Error");
                }
                ViewBag.AdTypes = entities.GetAdTypes();
                ViewBag.Categories = entities.GetCategories();
                ViewBag.Countries = entities.GetCountries();
                ViewBag.cid = id;
            }
            catch (ArgumentException ae)
            {
                ModelState.AddModelError("", ae.Message);
            }
            return View();
        }

        [Authorize(Roles = "Advertiser")]
        [HttpPost]
        public ActionResult AddAdvert(AdvertModel admodel, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    string path = "~/uploads/adverts/default.png";
                    Advertiser advertiser = entities.GetAdvertiser(entities.GetUser(User.Identity.Name));
                    if (file != null && file.ContentLength > 0)
                    {
                        string fileName = advertiser.ID + "_" + admodel.CampaignID + "_" + file.FileName.Trim().Replace(' ','_');
                        path = Path.Combine("~/uploads/adverts/", fileName);
                        file.SaveAs(Server.MapPath(path));
                    }
                    decimal costToAdvertiser = 0.1M;
                    decimal gainToBlogger = 0.05M;
                    entities.CreateAdvert(admodel.Name, advertiser.ID, path, admodel.TargetURL, admodel.CampaignID, int.Parse(admodel.CategoryID), int.Parse(admodel.AdTypeID), int.Parse(admodel.CountryID), admodel.Keywords, costToAdvertiser, gainToBlogger); //HARD CODED COUNTRY and COSTS!
                    return RedirectToAction("ShowCampaign", "Profile", new { id = admodel.CampaignID });
                }
                catch (ArgumentException ae)
                {
                    ModelState.AddModelError("", ae.Message);
                }
            }
            return View();
        }

        [Authorize(Roles = "Advertiser")]
        [HttpPost]
        public EmptyResult Upload(HttpPostedFileBase file)
        {
            Session["FilePath"] = null;
            // Verify that the user selected a file
            if (file != null && file.ContentLength > 0)
            {
                // extract only the fielname
                var fileName = Path.GetFileName(file.FileName);
                // store the file inside ~/App_Data/uploads folder
                var path = Path.Combine(Server.MapPath("~/App_Data/Adverts"), fileName);
                file.SaveAs(path);
                Session["FilePath"] = path;
            }
            // redirect back to the index action to show the form once again
            return new EmptyResult();
        }


    }
}
